package code;

import java.util.concurrent.*;

/**
 * @author xiaovcloud
 * @description:
 * @since 2021/8/19 8:18
 */
public class ThreadTest {
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		//继承Thread
		new MyThread().start();
		//实现Runnable
		new Thread(new MyRunnable()).start();
		//实现Callable
		FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
		new Thread(futureTask).start();
		System.out.println(futureTask.get());
		//线程池
		ExecutorService es = new ThreadPoolExecutor(3, 5,
				60, TimeUnit.MINUTES,
				new LinkedBlockingDeque<>(16),
				Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.AbortPolicy());
		es.submit(new MyRunnable());
		Future<Integer> future = es.submit(new MyCallable());
		System.out.println(future.get());

	}
}


class MyThread extends Thread {
	@Override
	public void run() {
		super.run();
		for (int i = 0; i < 10; i++) {
			System.out.println("my thread" + i);
		}
	}
}

class MyRunnable implements Runnable {
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("my runnable" + i);
		}
	}
}

class MyCallable implements Callable<Integer> {
	@Override
	public Integer call() throws Exception {
		int sum = 0;
		for (int i = 0; i < 10; i++) {
			sum += i;
		}
		return sum;
	}
}
